/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Like
 * Date: 2021-07-07
 * Time: 15:47
 */



import java.util.concurrent.*;

public class demoLatch {
        private static final int RUNNER_COUNT = 10;

        public static void main(String[] args) throws InterruptedException {
            final CountDownLatch begin = new CountDownLatch(1);
            final CountDownLatch end = new CountDownLatch(RUNNER_COUNT);
            final ExecutorService exec = new ThreadPoolExecutor(10,10,0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>());
            //必须执行10次后才停止
            for (int i = 0; i < RUNNER_COUNT; i++) {
                final int NO = i + 1;
                Runnable run = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            begin.await();
                            Thread.sleep((long)(Math.random() * 1000));
                            System.out.println("No." + NO + " arrived");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            end.countDown();
                        }
                    }
                };
                exec.submit(run);
            }

            System.out.println("Game Start ...");
            begin.countDown();
            end.await();
//        end.await(30, TimeUnit.SECONDS);
            System.out.println("Game Over.");

            exec.shutdown();
        }
    }

